package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 修改用户信息，根据用户输入的用户名修改该用户的密码和昵称
 * 使用预编译SQL语句完成
 */
public class JDBCDemo11 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入新密码");
        String password = scanner.nextLine();
        System.out.println("请输入新昵称");
        String nickname = scanner.nextLine();
        try (Connection connection = DBUtil.getConnection();){
            String sql = "UPDATE userinfo " +
                         "SET password=?,nickname=? " +
                         "WHERE username=?";

            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,nickname);
            ps.setString(3,username);
            int num = ps.executeUpdate();
            System.out.println(num>0?"修改成功":"修改失败");


        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
